package best.driver.android.View;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import android.widget.FrameLayout;
import best.driver.android.Beans.*;
import best.driver.android.R;
import best.driver.android.Utils.SizeUtils;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.Utils.Utils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class NewOrderView extends FrameLayout {

    private Context mContext;

    @InjectView(R.id.ll_new_order)
    LinearLayout mLinearNewOrder;

    @InjectView(R.id.fl_map)
    FrameLayout mFrameMap;

    @InjectView(R.id.tv_load_address)
    TextView mTvLoadAddress;

    @InjectView(R.id.best_map_view)
    BestMapView mMapView;

    @InjectView(R.id.tv_load_address_desc)
    TextView mTvLoadAddressDesc;

    @InjectView(R.id.tv_unload_address)
    TextView mTvUnloadAddress;

    @InjectView(R.id.mav_multi_address)
    MultiAddressView mMavMultiUnloadAddress;

    @InjectView(R.id.mav_multi_load_address)
    MultiAddressView mMavMultiLoadAddress;

    @InjectView(R.id.tv_unload_address_desc)
    TextView mTvUnloadAddressDesc;

    @InjectView(R.id.tv_familiar)
    TextView mTvFamiliar;

    @InjectView(R.id.tv_extra_info)
    TextView mTvExtraInfo;

    @InjectView(R.id.tv_mark)
    TextView mTvMark;

    @InjectView(R.id.tv_price)
    TextView mTvPrice;

    @InjectView(R.id.tv_price_desc)
    TextView mTvPriceDesc;

    @InjectView(R.id.tv_price_mark)
    TextView mTvPriceMark;

    @InjectView(R.id.tv_title_pre)
    TextView mTvTitlePre;

    @InjectView(R.id.tv_title_tail)
    TextView mTvTitleTail;

    @InjectView(R.id.fl_top_bar)
    FrameLayout mFlTopBar;

    @InjectView(R.id.iv_type_flag)
    ImageView mIvTypeFlag;

    @InjectView(R.id.tv_subsidy_pre)
    TextView mTvSubsidyPre;

    @InjectView(R.id.tv_subsidy)
    TextView mTvSubsidy;

    @InjectView(R.id.tv_subsidy_tail)
    TextView mTvSubsidyTail;

    @OnClick(R.id.ll_slide_down)
    public void showOrderDetail() {
        mLinearNewOrder.setVisibility(View.VISIBLE);
        mFrameMap.setVisibility(View.GONE);
    }

    @OnClick(R.id.ll_slide_up)
    public void showOrderMap() {
        mLinearNewOrder.setVisibility(View.GONE);
        mFrameMap.setVisibility(View.VISIBLE);
    }

    public NewOrderView(Context context) {
        this(context, null);
        mContext = context;
    }

    public NewOrderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.new_order_view, this, true);
        ButterKnife.inject(this, view);
    }

    private void setPrice(NewOrderInfo orderInfo) {
        if(orderInfo.familiarPrice > 0.1) {
            mTvPrice.setText(Util.getOnePointNumber(orderInfo.familiarPrice));
            hideSubsidy();
            return;
        }
        if(orderInfo.subsidy < 0.1) {
            mTvPrice.setText(Util.getOnePointNumber(orderInfo.estimatePrice));
            hideSubsidy();
        } else {
            showSubsidy();
            mTvPrice.setText(Util.getOnePointNumber(orderInfo.estimatePrice - orderInfo.subsidy));
            mTvSubsidy.setText(Util.getOnePointNumber(orderInfo.subsidy));
        }
    }

    private void showSubsidy() {
        mTvSubsidy.setVisibility(View.VISIBLE);
        mTvSubsidyPre.setVisibility(View.VISIBLE);
        mTvSubsidyTail.setVisibility(View.VISIBLE);
    }

    private void hideSubsidy() {
        mTvSubsidyPre.setVisibility(View.GONE);
        mTvSubsidy.setVisibility(View.GONE);
        mTvSubsidyTail.setVisibility(View.GONE);
    }

    private void setPickupAddress(String address, String addressDesc) {
        mTvLoadAddress.setText(address);
        if(!TextUtils.isEmpty(addressDesc)) {
            mTvLoadAddressDesc.setVisibility(VISIBLE);
            mTvLoadAddressDesc.setText(addressDesc);
        }
    }

    private void setUnloadAddress(String address, String addressDesc) {
        mTvUnloadAddress.setText(address);
        if(!TextUtils.isEmpty(addressDesc)) {
            mTvUnloadAddressDesc.setVisibility(View.VISIBLE);
            mTvLoadAddressDesc.setText(addressDesc);
        }
    }

    private void setExtraInfo(String extraInfo) {
        if(!TextUtils.isEmpty(extraInfo)) {
            mTvExtraInfo.setText(extraInfo);
        } else {
            mTvExtraInfo.setText("无");
        }
    }

    private void setMark(String mark) {
        if(!TextUtils.isEmpty(mark)) {
            mTvMark.setText(mark);
        } else {
            mTvMark.setText("无");
        }
    }

    private void setType(String type) {
        if(OrderStatus.BOOK.equals(type)) {
            mIvTypeFlag.setImageResource(R.drawable.tap_blue);
            mFlTopBar.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.tap_blue_big));
        } else {
            mIvTypeFlag.setImageResource(R.drawable.tap_orange);
            mFlTopBar.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.tap_orange_big));
        }
    }

    public void setNewOrderView(NewOrderInfo orderInfo) {
        LatLng startPos = new LatLng(0,0);
        LatLng endPos = new LatLng(0,0);
        if(orderInfo != null) {

            //额外服务
//            StringBuilder builder = new StringBuilder();
//            if(orderInfo.orderExtraServices != null && orderInfo.orderExtraServices.size() > 0) {
//                List<NewOrderInfo.OrderExtraServices> extraServices = orderInfo.orderExtraServices;
//                for(int i = 0; i < extraServices.size() - 1; i++) {
//                    builder.append(extraServices.get(i).displayName).append(",");
//                }
//                builder.append(extraServices.get(extraServices.size()-1).displayName);
//            }
//            if(!TextUtils.isEmpty(builder.toString())) {
//                setExtraInfo(builder.toString());
//            }
            if(!TextUtils.isEmpty(orderInfo.extraServiceDetail)) {
                setExtraInfo(orderInfo.extraServiceDetail);
            } else {
                setExtraInfo("无");
            }

            //显示接货卸货地址
            List<OrderAddresses> addresses = orderInfo.orderAddresses;
            List<OrderAddresses> shipperAddresses = new ArrayList<OrderAddresses>();
            List<OrderAddresses> receiverAddresses = new ArrayList<OrderAddresses>();
            final List<LatLng> passLatLngs = new ArrayList<LatLng>();

            if(addresses != null && addresses.size() > 0) {
                for(int i = 0; i < addresses.size(); i++) {
                    OrderAddresses address = addresses.get(i);
                    if(OrderStatus.SHIPPER_ADDRESS.equals(address.flag)) {
                        setPickupAddress(address.address, null);
                        startPos = new LatLng(address.latitude, address.longitude);
                        shipperAddresses.add(address);
                        if(OrderStatus.N_TO_ONE.equals(orderInfo.transportMode)) {
                            passLatLngs.add(new LatLng(address.latitude, address.longitude));
                        }
                    } else if (OrderStatus.RECEIVER_ADDRESS.equals(address.flag)) {
                        setUnloadAddress(address.address, null);
                        endPos = new LatLng(address.latitude, address.longitude);
                        receiverAddresses.add(address);
                        if(OrderStatus.ONE_TO_N.equals(orderInfo.transportMode)) {
                            passLatLngs.add(new LatLng(address.latitude, address.longitude));
                        }
                    }
                }
            }
            
            if(shipperAddresses.size() > 0) {
                mMavMultiLoadAddress.setDate(shipperAddresses);
            } 
            
            if(receiverAddresses.size() > 0) {
                mMavMultiUnloadAddress.setDate(receiverAddresses);
            }

            setPrice(orderInfo);
            if(!TextUtils.isEmpty(orderInfo.remark)) {
                setMark(orderInfo.remark);
            } else {
                setMark("无");
            }

            final LatLng start = startPos;
            final LatLng end = endPos;

            if (startPos.latitude > 1e-6 && startPos.longitude > 1e-6 && endPos.latitude > 1e-6 && endPos.longitude > 1e-6) {

                if(mMapView != null && mMapView.mBaiduMap != null) {
                    mMapView.mBaiduMap.clear();
                }
                if (passLatLngs.size() > 1) {
                    mMapView.planRoute(start, end, passLatLngs.subList(0, passLatLngs.size() - 1), orderInfo.transportMode);
                } else {
                    mMapView.planRoute(null, null, start, end, "start", "end");
                }
            }

            setType(orderInfo.type);

            if(OrderStatus.BOOK.equals(orderInfo.type)) {
                mTvTitlePre.setText("");
                mTvTitleTail.setText(TimeUtil.formatTime(orderInfo.bookTime));
            } else {
                mTvTitlePre.setText("距离");
                mTvTitleTail.setText(orderInfo.driverDistance + "米");
            }
            if(!TextUtils.isEmpty(PayType.getInstance().getPayTypeMap().get(orderInfo.paymentMethod))) {
                mTvPriceMark.setText(PayType.getInstance().getPayTypeMap().get(orderInfo.paymentMethod));
            }

            if(orderInfo.familiarFlag) {
                mTvFamiliar.setVisibility(View.VISIBLE);
            } else {
                mTvFamiliar.setVisibility(View.GONE);
            }

            if(orderInfo.familiarPrice > 0.1) {
                mTvPriceDesc.setText("一口价");
                setPrice(orderInfo);
            } else {
                mTvPriceDesc.setText("预估价格");
                setPrice(orderInfo);
            }


        }
    }

    public void hideListView(String flag) {
        if(OrderStatus.RECEIVER_ADDRESS.equals(flag)) {
            mMavMultiUnloadAddress.mLLAddressOneLine.setVisibility(View.VISIBLE);
            mMavMultiUnloadAddress.mLvMultiAddress.setVisibility(View.GONE);
        } else {
            mMavMultiLoadAddress.mLLAddressOneLine.setVisibility(View.VISIBLE);
            mMavMultiLoadAddress.mLvMultiAddress.setVisibility(View.GONE);
        }
    }



}
